package www.geek.filemanager.base;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;

import butterknife.ButterKnife;
import www.geek.filemanager.utils.ToastUtils;


public abstract class BaseActivity extends Activity implements
        View.OnClickListener, BaseInterface {


    protected LayoutInflater mInflater;

    protected Dialog netDialog;

    private final int HANDLER_MSG_TAG_NETDIALOG_SHOW = 11110;

    private final int HANDLER_MSG_TAG_NETDIALOG_CANCLE = 22220;

    private String mStr_netDialogMsg = "";

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onBeforeSetContentLayout();
        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
        }

        ButterKnife.bind(this);
        mInflater = getLayoutInflater();

        init(savedInstanceState);

        initView();
        initData();
    }


    protected void onBeforeSetContentLayout() {
    }

    protected int getLayoutId() {
        return 0;
    }

    protected View inflateView(int resId) {
        return mInflater.inflate(resId, null);
    }

    protected void init(Bundle savedInstanceState) {
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    /**
     * 发handler通知UI消息
     *
     * @param _what
     */
    protected void sendMsg(int _what) {
        Message msg = handler.obtainMessage();
        msg.what = _what;
        handler.sendMessage(msg);
    }

    public void showNetDialog() {
        this.mStr_netDialogMsg = "正在加载数据...";
        sendMsg(HANDLER_MSG_TAG_NETDIALOG_SHOW);
    }


    public void showNetDialog(String _context) {
        this.mStr_netDialogMsg = _context;
        sendMsg(HANDLER_MSG_TAG_NETDIALOG_SHOW);
    }

    public void cancelNetDialog() {
        sendMsg(HANDLER_MSG_TAG_NETDIALOG_CANCLE);
    }


    private void initNetDialog() {
        netDialog = ToastUtils.showToastDialogNoClose(this, this.mStr_netDialogMsg,
                ToastUtils.TOAST_LOADING, true);
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case HANDLER_MSG_TAG_NETDIALOG_SHOW:
                    if (netDialog == null) {
                        initNetDialog();
                    }
                    if (!netDialog.isShowing()) {
                        netDialog.show();
                    }
                    break;
                case HANDLER_MSG_TAG_NETDIALOG_CANCLE:
                    if (netDialog == null) {
                        return;
                    }
                    if (netDialog.isShowing())
                        netDialog.dismiss();
                    break;
                default:
                    handlerMsg(msg);
                    break;
            }
        }
    };

    /**
     * 子类可以重写此函数，处理自己的UI
     *
     * @param msg
     */
    protected void handlerMsg(Message msg) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            backKeyDown();
        }
        return super.onKeyDown(keyCode, event);
    }

    protected abstract void backKeyDown();
}
